package com.choosefine.springboot.devops.base.common.bean;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by 邱鹏 on 2016/9/24.
 */
public class CommonToolkitBean {
    @Autowired
    public SimpleTemplateBean simpleTemplateBean;
    private final ObjectMapper mapper = new ObjectMapper();

    /**
     * 从request中获得参数Map
     *
     * @param request
     * @return 普通的map
     */
    public Map requestParamAsMap(HttpServletRequest request) {
        // 参数Map
        Map properties = request.getParameterMap();
        // 返回值Map
        Map returnMap = new HashMap();
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }

    /**
     * 调用simpleTemplate的模版转换方法
     *
     * @param src
     * @param data
     * @param type
     * @return
     */
    public String template(String src, Map<String, Object> data, SimpleTemplateBean.TemplateType type) {
        return simpleTemplateBean.template(src, data, type);
    }

    /***
     * 将对象转化成Map
     *
     * @param obj
     * @return
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     */
    public Map convertObjToMap(Object obj) throws NoSuchFieldException, IllegalAccessException {
        Map<String, Object> reMap = new HashMap<String, Object>();
        if (obj == null) {
            reMap = null;
        } else {
            Field[] fields = obj.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                Field f = obj.getClass().getDeclaredField(fields[i].getName());
                f.setAccessible(true);
                Object o = f.get(obj);
                reMap.put(fields[i].getName(), o);
            }
        }
        return reMap;
    }

    /****
     * @param map
     * @param object
     * @return
     * @throws IOException
     */
    public Object mapToObject(Map<String, Object> map, Object object) throws IOException {
        String mapJson = mapper.writeValueAsString(map);
        object = mapper.readValue(mapJson, object.getClass());
        return object;
    }

    /**
     * 将 String 转为 map
     *
     * @param param aa=11&bb=22&cc=33
     * @return
     */
    public Map<String, Object> getUrlParamsMap(String param) {
        Map<String, Object> map = new HashMap<String, Object>();
        if ("".equals(param) || null == param) {
            return map;
        }
        String[] params = param.split("&");
        for (int i = 0; i < params.length; i++) {
            String[] p = params[i].split("=");
            if (p != null && p.length == 2) {
                map.put(p[0], p[1]);
                //TODO:此处存在风险，如果传入字符串中包含重复的key该如何处理？
                //TODO:如何处理传进数组的情况呢
            }
        }
        return map;
    }

}
